<?php
App::uses('Controller', 'Controller');

class AppController extends Controller {	
	public $components = array(
        'Session',
        'Auth' => array(		
            "authenticate" => array(
                "all" => array("userModel" => "Customer"),
                "Form"      => array(
                    "fields" => array("username" => "username",'password' => 'password','del_flag'=>0),                    
                )
            ),
            'loginAction' => array('controller' => 'customers', 'action' => 'login'),
            'loginRedirect' => array('controller' => 'pages', 'action' => 'display','home'),
            'logoutRedirect' => array('controller' => 'customers', 'action' => 'login'),
			'authError' => 'Bạn không có quyền xem trang này',			
        )
    );
	public $helpers = array('Html','Form','Session');
	public $uses = array('Brand','Category');
	
	function beforeFilter () {
        $this->Auth->autoRedirect = false;
    }
	
	function beforeRender(){
		parent::beforeRender();
		if($current_user = $this->Auth->user()){
			$this->set(compact('current_user'));
		}
		$brands = $this->Brand->find('all',array(
			'fields'=>array('Brand.name','Brand.id'),
			'joins' => array(
				array(
					'table' => 'products',
					'alias' => 'Product',
					'conditions' => array('Brand.id = Product.brand_id'),
					'type' => 'inner',
				),
			),
			'conditions'=>array(
				'Brand.del_flag' => 0,
			),
			'order' => 'orderby asc',
			'group' => 'Brand.id',
		));
		$categories = $this->Category->find('all',array(
			'fields' => array('Category.*'),
			/*'joins' => array(
				array(
					'table' => 'products',
					'alias' => 'Product',
					'conditions' => array('Category.id = Product.category_id'),
					'type' => 'inner',
				),
			),*/
			'conditions' => array(
				'Category.del_flag' => 0,
			),
			'order' => 'lft asc',
			'group' => 'Category.id',
		));
		$this->set(compact('brands','categories'));
	}
	
	function construct()
	{
		parent::construct();
		$path = func_get_args();

		$count = count($path);
		if (!$count) {
			$this->redirect('/');
		}
		$title_for_layout = null;

		if (!empty($path[$count - 1])) {
			$title_for_layout = Inflector::humanize($path[$count - 1]);
		}
		$this->set(compact('title_for_layout'));
		$this->render(implode('/', $path));
	}
}
